package pl.us.ij.plugins.segmentation.commands.threshold;

import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import pl.us.ij.plugins.segmentation.Trace;
import pl.us.ij.plugins.segmentation.infrastructure.Command;
import pl.us.ij.plugins.segmentation.infrastructure.exceptions.BreakCommandException;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-02-27
 * Time: 14:07:48
 * To change this template use File | Settings | File Templates.
 */
public class OneLevelThresholdCommand extends Command<OneLevelThresholdCommandParams> {
    @Override
    protected void processImage(FloatProcessor fp) throws BreakCommandException {

        float threshold = getParams().getThreshold();

        ImageProcessor ip = fp;

        fp.findMinAndMax();

        float[] pixels = (float[]) ip.getPixels();

        Trace.log("Min: " + ip.getMin());
        Trace.log("Max: " + ip.getMax());

        thresholdPixels(threshold, pixels, (float) ip.getMin(), (float) ip.getMax());
    }

    public static void thresholdPixels(float threshold, float[] pixels, float min, float max) {

        Trace.log("thresholdPixels(Threshold:" + threshold + ", ...)");

        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] <= threshold)
                pixels[i] = min;
            else
                pixels[i] = max;
        }
    }
}
